home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / disk / misc / mirror.lha / Mirror.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  3.2 KB  |  139 lines

  1. /********** MIRROR.C **********/                /* :ts=8 */
  2.  
  3.  
  4. /* This simple tool changes the directory worked in:
  5.    It searches for the same directory on 'the' alternate drive.
  6.    This way, two drives can be used where one holds programs and the
  7.    other holds data, but always in the same tree structure.
  8.    Normally, if a part of the structure misses, the closest available
  9.    parent is chosen. In CREATEive mode, the program will act differently 
  10.    by creating the directory on the destination side.
  11. */
  12.  
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <exec/types.h>
  17. #include <libraries/dos.h>
  18. #include <pragmas/dos_lib.h>
  19. #include <clib/dos_protos.h>
  20.  
  21.  
  22. typedef BPTR LOCK;
  23.  
  24.  
  25. int Bstrcmp (char *b1, char *b2)
  26.  {
  27.    int i=1+*b1;
  28.    while (i--)
  29.       if (*b1++ != *b2++)
  30.          return 1;
  31.    return 0;
  32.  }
  33.  
  34. int Mirror (LOCK fl, LOCK from, LOCK to, int crea)
  35.  {
  36.    LOCK pl=(fl==0L)? 0L: ParentDir (fl);
  37.  
  38.    if (pl==0L)    /* Recursion ends, and fl is volume name */
  39.     {
  40.       UnLock (CurrentDir (to));
  41.       UnLock (from);
  42.       UnLock (fl);
  43.       return 0;        /* Still exactly the same */
  44.     }
  45.    else
  46.     {
  47.       static struct FileInfoBlock fib;
  48.  
  49.       if (Mirror (pl, from, to, crea))
  50.          return 1;    /* Not the same anymore */
  51.       Examine (fl, &fib);
  52.       UnLock (fl);
  53.       if (fl=Lock (fib.fib_FileName, ACCESS_READ))
  54.        {
  55.          UnLock (CurrentDir (fl));
  56.      return 0;    /* Still exactly the same */
  57.        }
  58.       else
  59.          if (crea)
  60.       {
  61.         fl=CreateDir (fib.fib_FileName);
  62.         if (fl)
  63.          {
  64.            UnLock (fl);    /* It's exclusive */
  65.            if (fl=Lock (fib.fib_FileName, ACCESS_READ))
  66.             {
  67.           UnLock (CurrentDir (fl));
  68.           return 0;    /* Still exactly the same */
  69.         }
  70.            else
  71.             {
  72.           fputs ("Mirror: New directory disappeared\n", stderr);
  73.           exit (1);
  74.         }
  75.          }
  76.         else
  77.          {
  78.            fputs ("Mirror: Failed to create directory\n", stderr);
  79.            exit (1);
  80.          }
  81.       }
  82.      else
  83.       {
  84.         return 1;    /* Difference came up */
  85.       }
  86.     }
  87.  }
  88.  
  89. void main (int argc, char *argv [])
  90.  {
  91.    int crea=(argc==4), err=0;
  92.  
  93.    if (argc<3)
  94.     {
  95.       fputs ("Mirror: Enter two arguments for disknames.\n"
  96.       "\tA third word being CREATE on the line causes creative mode.\n", stderr);
  97.       err=1;
  98.     }
  99.    if (argc>4)
  100.     {
  101.       fputs ("Mirror: Too many arguments: Enter two disks and "
  102.               "optional CREATE\n", stderr);
  103.       err=1;
  104.     }
  105.    if (!err)
  106.     {
  107.       LOCK fl, fl1, fl2, from, to;
  108.       static struct InfoData id, id1;
  109.  
  110.       if (fl=Lock ("", ACCESS_READ))
  111.        {
  112.          if (fl1=Lock (argv [1], ACCESS_READ))
  113.       {
  114.         if (fl2=Lock (argv [2], ACCESS_READ))
  115.          {
  116.            Info (fl,  &id );
  117.            Info (fl1, &id1);
  118. /*           if (Bstrcmp (BADDR(id.id_VolumeNode), BADDR(id1.id_VolumeNode)))
  119. */
  120.            if (id.id_VolumeNode!=id1.id_VolumeNode)
  121.                            /* if TRUE: CurDrv!=FirstDrv */
  122.         { from=fl2; to=fl1; }    /* GOTO FirstDrv */
  123.            else
  124.             { from=fl1; to=fl2; }    /* GOTO SecondDrv */
  125.            if (Mirror (fl, from, to, crea))
  126.               fputs ("Mirror: No exact matching of directories\n",
  127.                           stderr);
  128.            fl=fl1=fl2=0L;
  129.          }
  130.         else fputs ("Cannot Lock second drive\n", stderr);
  131.         if (fl1) UnLock (fl1);
  132.       }
  133.      else fputs ("Cannot Lock first drive\n", stderr);
  134.      if (fl) UnLock (fl);
  135.        }
  136.       else fputs ("Cannot Lock current directory\n", stderr);
  137.     }
  138.  }
  139.